package com.songcha.sakura.mvvm.downloading

import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.songcha.library_base.mvvm.base.BaseRepository
import com.songcha.library_base.mvvm.base.BaseViewModel
import com.songcha.sakura.download.*
import com.songcha.sakura.download.base.DownloadTask
import com.songcha.sakura.download.base.TaskState


class DownloadingViewModel(app: Application) : BaseViewModel<DownloadingRepository>(app) {
     var taskList:MutableLiveData<MutableList<DownloadTask>> = MutableLiveData()
     //private var taskListValue=taskList.value
     var oldTaskList:MutableList<DownloadTask> = mutableListOf()

     //val taskList = MutableLiveData<MutableList<Task>>()

    init {
        taskList.value= mutableListOf()
    }

    fun pauseAllTask(){
       /* if(taskList.value==null) return
        val tmp=mutableListOf<DownloadTask>()
        tmp.addAll(taskList.value!!)*/

        val tmp=taskList.value?:return

        for(i in tmp.indices){
            tmp[i].state= TaskState.PAUSE
        }
        taskList.value=tmp
    }

    fun updateTaskList(list: MutableList<DownloadTask>){
        /*val tmp=mutableListOf<DownloadTask>()
        tmp.addAll(list)*/
        val tmp=taskList.value?:return
        tmp.clear()
        tmp.addAll(list)
        //taskListValue.clear()
        //taskListValue.addAll(list)
        taskList.value=tmp
    }

    fun updateOldTaskList(){
        /*if(taskList.value==null) return
        val tmp=mutableListOf<DownloadTask>()
        tmp.addAll(taskList.value!!)*/
        val tmp=taskList.value?:return

        oldTaskList.clear()
        oldTaskList.addAll(tmp)
    }

    fun notifiyItemChanged(t:DownloadTask){
   /*     if(taskList.value==null) return
        val tmp=mutableListOf<DownloadTask>()
        tmp.addAll(taskList.value!!)*/
        val tmp=taskList.value?:return
        val idx=isInTaskList(t)

        //LogUtil.log("notifiyItemChanged",idx,t.state.name)

        if(idx>-1){
            tmp[idx]=t
            //diffUpdate()
            taskList.value=tmp
        }

    }

    fun addItem(task:DownloadTask){
       /* if(taskList.value==null) return
        val tmp=mutableListOf<DownloadTask>()
        tmp.addAll(taskList.value!!)*/
        val tmp=taskList.value?:return

        val idx=isInTaskList(task)
        if(idx>-1){
            tmp[idx]=task
        }else{
            tmp.add(0,task)
        }
        taskList.value=tmp
        //diffUpdate()
    }

    fun removeItem(task:DownloadTask){
       /* if(taskList.value==null) return
        val tmp=mutableListOf<DownloadTask>()
        tmp.addAll(taskList.value!!)*/
        val tmp=taskList.value?:return

        val idx=isInTaskList(task)
        if(idx>-1){
            tmp.removeAt(idx)
            taskList.value=tmp
            //diffUpdate()
        }
    }


    private fun isInTaskList(t:DownloadTask):Int{
      /*  if(taskList.value==null) return -1
        val tmp=mutableListOf<DownloadTask>()
        tmp.addAll(taskList.value!!)*/
        val tmp=taskList.value?:return -1

        for(i in tmp.indices){
            if(tmp[i].url==t.url){
                return i
            }
        }
        return -1
    }



}